import Vue from 'vue'
import Router from 'vue-router'

const Home = () => import('../views/Home.vue')
const Video = () => import('../views/Video.vue')
const Tutorials = () => import('../views/Tutorials.vue')
const Commodity = () => import('../views/Commodity.vue')
const Login = () => import('../views/Login.vue')
const My = () => import('../views/My.vue')
const Search = () => import('../views/Search.vue')
const PerInfo = () => import('../views/PerInfo.vue')
const RepairSecret = () => import('../views/RepairSecret.vue')
const IdiomList = () => import('../views/IdiomList.vue')
const VideoDetail = () => import('../views/VideoDetail.vue')
const AlreadyVideo = () => import('../views/AlreadyVideo.vue')
const MyHarvest = () => import('../views/MyHarvest.vue')
const Wonderful = () => import('../views/Wonderful.vue')
const CommLink = () => import('../views/CommLink.vue')
const LeaveMsg = () => import('../views/LeaveMsg.vue')

Vue.use(Router)

// 这段代码解决 路由重复点击报错
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

let router = new Router({
  mode: 'history',
  routes: [
    {
      path: '',
      redirect: 'Home',
      meta: {
        keepAlive: true //不需要被缓存的组件
      }
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      meta: {
        keepAlive: true 
      }
    },
    {
      path: '/video',
      name: 'Video',
      component: Video,
      meta: {
        keepAlive: false
      }
    },
    {
      path: '/tutorials',
      name: 'Tutorials',
      component: Tutorials,
      meta: {
        keepAlive: false
      }
    },
    {
      path: '/commodity',
      name: 'Commodity',
      component: Commodity,
      meta: {
        keepAlive: false
      }
    },
    {
      path: '/my',
      name: 'My',
      component: My,
      meta: {
        keepAlive: true
      }
    },
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta: {
        keepAlive: true
      }
    },
    {
      path: '/search',
      name: 'Search',
      component: Search,
      meta: {
        keepAlive: true
      }
    },
    {
      path: '/perInfo',
      name: 'PerInfo',
      component: PerInfo,
      meta: {
        keepAlive: true
      }
    },
    {
      path: '/repairSecret',
      name: 'RepairSecret',
      component: RepairSecret,
      meta: {
        keepAlive: true
      }
    },
    {
      path: '/idiomList',
      name: 'IdiomList',
      component: IdiomList,
      meta: {
        keepAlive: false
      }
    },
    {
      path: '/videoDetail',
      name: 'VideoDetail',
      component: VideoDetail,
      meta: {
        keepAlive: false
      }
    },
    {
      path: '/alreadyVideo',
      name: 'AlreadyVideo',
      component: AlreadyVideo,
      meta: {
        keepAlive: true
      }
    },
    {
      path: '/myHarvest',
      name: 'MyHarvest',
      component: MyHarvest,
      meta: {
        keepAlive: true
      }
    },
    {
      path: '/wonderful',
      name: 'Wonderful',
      component: Wonderful,
      meta: {
        keepAlive: false
      }
    },
    {
      path: '/commLink',
      name: 'CommLink',
      component: CommLink,
      meta: {
        keepAlive: true
      }
    },
    {
      path: '/leaveMsg',
      name: 'LeaveMsg',
      component: LeaveMsg,
      meta: {
        keepAlive: true
      }
    },

  ]
})

// 全局导航守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.path === '/login' || token) {
    next()
  } else {
    next('/login')
  }
})
 
export default router
